<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\api\controller;

use app\admin\model\Team;
use app\common\constant\Constant;
use app\common\controller\Api;
use app\common\library\Authentication\Aliauth;
use app\common\library\InviteCode;
use app\common\library\LuckyCola;
use app\common\library\Map\Map;
use app\common\library\Wechat;
use app\common\manager\BaseManager;
use app\common\manager\MatchManager;
use app\common\manager\PublicManager;
use app\common\manager\RegionManager;
use app\common\manager\UserAccountManager;
use app\common\model\Area;
use app\common\model\Config;
use app\common\model\Merchant;
use app\common\model\Notice;
use app\common\model\PayLog;
use app\common\model\Region;
use app\common\model\Reward;
use app\common\model\User;
use app\common\model\UserAccount;
use app\common\model\UserTeam;
use think\Db;
use think\Exception;
use Yansongda\Pay\Plugin\Alipay\Trade\PageRefundPlugin;
use function fast\e;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['index','test','getRegion','config','notice','noticeList','information'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function test()
    {
        /*$id = '510683200211016935';
        $year = substr($id,6,4);
        $month = substr($id,10,2);
        $day = substr($id,12,2);
        $birthday = $year.'-'.$month.'-'.$day;
        echo $birthday;
        User::where('id',1)->update(['birthday'=>$birthday]);
        exit;
        //echo 'https://apis.map.qq.com/ws/geocoder/v1/?location='.urlencode('39.984154,116.307490').'&key='.urlencode('ZCABZ-DYKCT-IGNXJ-VGMHO-7OVSH-VXB36').'&get_poi=1';
        $result = Aliauth::main('刘绪涛','510683200211016935','15196356090');
        print_r($result);
        exit;*/
        $this->success('测试成功');
    }

    public function getCultivate(){
        $config = [
            [
                'name'=>'助教',
                'value'=>1,
                'description'=>'助教培训'
            ],
            [
                'name'=>'赛事',
                'value'=>2,
                'description'=>'赛事培训',
            ],
            [
                'name'=>'裁判',
                'value'=>3,
                'description'=>'裁判培训',
            ],
        ];

        $this->success('获取成功',$config);
    }

    /**
     * 获取省市区信息
     * @param $pid 父级 city_code
     * @param $type 级别 1=省,2=市,3=区
     * @return array
     * @throws \Exception
     */
    public function getRegion()
    {
        $type = $this->request->post('type') ?? 1;
        $pid = $this->request->post('pid');
        try {
            $result  = RegionManager::getRegion($pid,$type);
            if ($result['code']!=1){
                throw new \Exception($result['message']);
            }
            $this->success($result['message'],$result['data']);
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }
    public function config()
    {
        $config = config('redis');
        $redis = new \Redis();
        $redis->connect($config['host'],$config['port']);
        if ($config['password']){
            $redis->auth($config['password']);
        }
        $data = $redis->get(Constant::getConfigKey($config['prefix']));
        if ($data){
            $data = json_decode($data,true);
        }else{
            $data = Config::where('group','site')->field('title,name,value,extend,type')->select();
            if ($data){
                $redis->set(Constant::getConfigKey($config['prefix']),json_encode($data));
            }
        }
        $show = [];
        foreach ($data as $k=>$v){
            if ($v['type']=='image' && strrpos($v['value'],'http')===false){
                $data[$k]['value'] = cdnurl($v['value']);
            }

            $show[$k] = ['type'=>$v['type'],'value'=>$data[$k]['value'],'name'=>$v['name'],'title'=>$v['title'] ];
        }
        $this->success('获取成功',array_values($show));
    }

    public function information()
    {
        $page = $this->request->param('page');
        try {
            $result = PublicManager::information($page);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    public function notice()
    {
        try {
            $notice = Notice::order('id desc')->find();
            $this->success('获取成功',$notice);
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    public function noticeList()
    {
        try {
            $page = $this->request->get('page') ?? 1;
            if ($page < 1){
                $page = 1;
            }
            $step = Constant::PAGE_LIMIT;
            $start = ($page-1)*$step;
            $notice = Notice::where('status',Notice::STATUS_SHOW)->order('id desc')->limit($start,$step)->select();
            $count = Notice::where('status',Notice::STATUS_SHOW)->order('id desc')->count();
            $return['list'] = $notice;
            $pageData = page($count,$step,$page);
            $data = array_merge($return,$pageData);
            $this->success('获取成功',$data);
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    public function moneyList()
    {
        try {
            $page = $this->request->post('page') ?? 1;
            if ($page < 1){
                $page = 1;
            }
            $step = Constant::PAGE_LIMIT;
            $start = ($page-1)*$step;
            $list = Reward::order('id desc')->limit($start,$step)->select();
            $count = Reward::count();
            $return['list'] = $list;
            $pageData = page($count,$step,$page);
            $data = array_merge($return,$pageData);
            $this->success('获取成功',$data);
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }
}
